<?php
Class MenuAction extends AdminBaseAction {

	public function index() {
		$data = $this->getMenuTree();
		$this->assign('data',$data);
		$this->display();
	}

	/**
	 * 添加菜单
	 */
	public function add() {
		if(IS_POST) {
			if(!isset($_POST['url']) || empty(trim($_POST['url'])) ) {
				$this->error('URL不能为空!');
			}else{
				$_POST['url'] = strtolower($_POST['url']);
			}
			if(false != D('Menu')->addMenu($_POST)) {
				$this->success('添加成功！');
			}else{
				$msg = D('Menu')->getError();
				$this->error( $msg ? $msg : '添加失败！');
			}
		}else{
			$menuArr = $this->getMenuTree(array('status'=>1));
			$this->assign('menuArr' ,$menuArr);
			$this->display();
		}
		
	}

	/**
	 * 编辑菜单
	 * @return [type] [description]
	 */
	public function edit() {
		if(IS_POST) {
			$id = I('post.id' ,'0' , 'intval');
			if(!isset($_POST['id']) || empty($id)) {
				$this->error('ID不能为空!');
			}
			if(false != D('Menu')->editMenu($_POST)) {
				$this->success('修改成功！');
			}else{
				$msg = D('Menu')->getError();
				$this->error( $msg ? $msg : '修改失败！');
			}
		}else{
			$id = I('get.id' , '' ,'intval');
			if(!empty($id)) {
				$data = D('Menu')->getOne($id);
				if(empty($data)) {
					$this->error('数据已经不存在!');
				}
			}else{
				$this->error('参数错误!');
			}
			$menuArr = $this->getMenuTree(array('status'=>0));

			$this->assign('menu' , $data);
			$this->assign('menuArr' ,$menuArr);
			$this->display();	
		}
		
	}

	/**
	 * [delete description]
	 * @return [type] [description]
	 */
	public function delete() {

	}
	public function getMenuTree($map) {
		$menuArr = D('Menu')->getList($map);
		$menuArr = toFormatTree($menuArr);
		return $menuArr;
	}


	
}